// 测试统计API
const axios = require('axios')

const API_BASE = 'http://localhost:5000/api'

async function testStatsAPI() {
  try {
    console.log('🔍 测试统计API...')
    
    // 1. 先测试登录获取token
    console.log('\n1. 测试登录...')
    const loginResponse = await axios.post(`${API_BASE}/auth/login/email`, {
      email: 'admin@aide.com',
      password: 'admin123456'
    })
    
    if (!loginResponse.data.success) {
      console.error('❌ 登录失败:', loginResponse.data.error)
      return
    }
    
    const token = loginResponse.data.token
    console.log('✅ 登录成功，获取到token')
    
    // 2. 测试概览统计
    console.log('\n2. 测试概览统计...')
    const overviewResponse = await axios.get(`${API_BASE}/stats/overview`, {
      headers: { Authorization: `Bearer ${token}` }
    })
    console.log('概览统计结果:', overviewResponse.data)
    
    // 3. 测试分类分布
    console.log('\n3. 测试分类分布...')
    const categoryResponse = await axios.get(`${API_BASE}/stats/category-distribution`, {
      headers: { Authorization: `Bearer ${token}` }
    })
    console.log('分类分布结果:', categoryResponse.data)
    
    // 4. 测试时间趋势
    console.log('\n4. 测试时间趋势...')
    const trendsResponse = await axios.get(`${API_BASE}/stats/time-trends?days=7`, {
      headers: { Authorization: `Bearer ${token}` }
    })
    console.log('时间趋势结果:', trendsResponse.data)
    
    // 5. 测试效率分析
    console.log('\n5. 测试效率分析...')
    const efficiencyResponse = await axios.get(`${API_BASE}/stats/efficiency`, {
      headers: { Authorization: `Bearer ${token}` }
    })
    console.log('效率分析结果:', efficiencyResponse.data)
    
  } catch (error) {
    console.error('❌ 测试失败:')
    if (error.response) {
      console.error('  状态码:', error.response.status)
      console.error('  响应数据:', error.response.data)
    } else if (error.request) {
      console.error('  请求失败:', error.message)
    } else {
      console.error('  错误:', error.message)
    }
  }
}

testStatsAPI()